/**
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef PANDA_TOOLING_TEST_JS_BREAKPOINT_TEST_H
#define PANDA_TOOLING_TEST_JS_BREAKPOINT_TEST_H

#include "test_util.h"

namespace ark::tooling::test {
class JsBreakpointTest : public ApiTest {
public:
    JsBreakpointTest()
    {
        vmStart = [this] {
            // NOLINTNEXTLINE(readability-magic-numbers)
            location_ = TestUtil::GetLocation("Sample.js", 22, pandaFile_.c_str());
            ASSERT_TRUE(location_.GetMethodId().IsValid());
            return true;
        };

        breakpoint = [this](PtThread, Method *, const PtLocation &location) {
            ASSERT_TRUE(location.GetMethodId().IsValid());
            ASSERT_LOCATION_EQ(location, location_);
            ++breakpointCounter_;
            return true;
        };

        loadModule = [this](std::string_view moduleName) {
            if (moduleName.find(pandaFile_.c_str()) == std::string_view::npos) {
                return true;
            }
            ASSERT_SUCCESS(debugInterface->SetBreakpoint(location_));
            auto error = debugInterface->SetBreakpoint(location_);
            ASSERT_FALSE(!error);
            return true;
        };

        vmDeath = [this]() {
            ASSERT_EQ(breakpointCounter_, 2U);
            return true;
        };
    }

    std::pair<const char *, const char *> GetEntryPoint() override
    {
        return {pandaFile_.c_str(), entryPoint_.c_str()};
    }

private:
    std::string pandaFile_ = "js/Sample.abc";
    std::string entryPoint_ = "_GLOBAL::func_main_0";
    PtLocation location_ {nullptr, PtLocation::EntityId(0), 0};
    size_t breakpointCounter_ = 0;
};

inline std::unique_ptr<ApiTest> GetJsBreakpointTest()
{
    return std::make_unique<JsBreakpointTest>();
}
}  // namespace ark::tooling::test

#endif  // PANDA_TOOLING_TEST_JS_BREAKPOINT_TEST_H
